const { Op } = require("sequelize");
const projectDetailsBranch = require("../model/projectDetailsBranch")

class projectDetailsBranchService {
    // 创建项目分支
    async createBranchProject({ id, branchId, projectName, projectLeader }) {
        try {
            const res = await projectDetailsBranch.create({
                projectId: id,
                branchId,
                projectName,
                projectLeader
            })
            return res
        } catch (error) {
            console.log(error);
            return error
        }
    }
    // 获取项目分支
    async getBranchProject({ projectId }) {
        try {
            const res = await projectDetailsBranch.findAll({
                where: {
                    projectId
                }
            })
            return res
        } catch (error) {
            console.log(error);
            return error
        }
    }
    // 分页 获取项目分支
    async getBranchProjectByPage({ projectId, pageSize, currentPage }) {
        try {
            const res = await projectDetailsBranch.findAndCountAll({
                where: {
                    projectId
                },
                limit: pageSize,
                offset: (currentPage - 1) * pageSize
            })
            return res
        } catch (error) {
            console.log(error);
            return error
        }
    }
    // 删除项目分支 将isDelte字段置为true
    async deleteBranchProject({ projectId, branchId }) {
        try {
            const res = await projectDetailsBranch.update({
                isDelte: true
            }, {
                where: {
                    projectId,
                    branchId
                }
            })
            return res
        } catch (error) {
            console.log(error);
            return error
        }
    }
    // 通过主项目id或者分支id获取项目分支
    async getBranchProjectById({ projectId }) {
        try {
            const res = await projectDetailsBranch.findAll({
                where: {
                    projectId
                }
            })
            return res
        } catch (error) {
            console.log(error);
            return error
        }
    }
}

module.exports = projectDetailsBranchService;
